/*
 * DataImporter.java
 *
 * Created on May 26, 2007, 5:39 PM
 *
 *  APPLICATION NAME
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger.entity;

import com.semanticvoid.lifelogger.configuration.ConfigManager;
import com.semanticvoid.lifelogger.configuration.Constants;
import com.semanticvoid.lifelogger.db.DbInterface;
import com.semanticvoid.lifelogger.html.parser.HTMLParser;
import com.semanticvoid.lifelogger.http.ContentFetcher;
import com.semanticvoid.lifelogger.xml.parser.DeliciousXMLReader;
import com.semanticvoid.lifelogger.xml.parser.GoogleReaderHistoryXMLReader;
import com.semanticvoid.lifelogger.xml.parser.GoogleWebHistoryXMLReader;
import java.util.ArrayList;
import java.util.Vector;

/**
 *
 * @author anand kishore
 */
public class DataImporter {
    
        public static void fetchHTMLContent() {
        ConfigManager mgr = ConfigManager.getInstance();
        
        // Fetch the content
        if(mgr.isFetchContent()) {
            System.out.println("Content Fetcher: Starting...");
            Vector<BrowseHistoryEntity> bEntities = DbInterface.getListOfBrowseItemsWithNoContent();
            
            ArrayList<Thread> threads = new ArrayList<Thread>();
                        
            for(final BrowseHistoryEntity bEntity : bEntities) {
                Thread t = new Thread() {
                    public void run() {
                        String htmlContent = ContentFetcher.fetch(bEntity.getLink());
                        String parsedContent = null;
                        
                        if(htmlContent != null) {
                            if(!htmlContent.equals("")) {
                                parsedContent = HTMLParser.parse(htmlContent);
                                bEntity.setContent(parsedContent);
                                
                                if(DbInterface.writeContent(bEntity.getGuid(), bEntity.getContent(), bEntity.extractTermVectorAsString())) {
                                    System.out.println("Wrote content for guid:" + bEntity.getGuid() + " link: " + bEntity.getLink());
                                }
                            }
                        } else {
                            DbInterface.markInvalid(bEntity.getGuid());
                        }
                    }
                };
                threads.add(t);
                t.start();
                               
                if(threads.size() == 1) {
                    for(Thread thread : threads) {
                        try {
                            thread.join();
                            Thread.sleep(1000);
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                    }
                    
                    threads.clear();
                }
            }
        }
    }

    public static void importGoogleReader() {
        ConfigManager mgr = ConfigManager.getInstance();
        
        // Read and purhe the Google Reader xml
        GoogleReaderHistoryXMLReader googRdrReader = new GoogleReaderHistoryXMLReader(mgr.getValueForKey(Constants.KEY_FOR_GOOGLEREADER_HISTORY_XML_PATH));
        if(googRdrReader.formDocuments()) {
            System.out.println("Google Reader: document formaton     [ OK ]");
            
            Vector<TaggedBrowseHistoryEntity> bEntities = googRdrReader.getTaggedObjects();
            if(DbInterface.purgeTaggedEntitiesToDb(bEntities)) {
                System.out.println("Google Reader: Bookmarks purge     [ OK ]");
            } else {
                System.out.println("Google Reader: Bookmarks purge     [ FAILED ]");
            }
        } else {
            System.out.println("Google Reader: document formaton     [ FAILED ]");
        }
    }

    public static void importDelicious() {
        ConfigManager mgr = ConfigManager.getInstance();
        
        // Read and purge the del.icio.us xml
        DeliciousXMLReader delReader = new DeliciousXMLReader(mgr.getValueForKey(Constants.KEY_FOR_DELICIOUS_XML_PATH));
        if(delReader.formDocuments()) {
            System.out.println("Del.icio.us: document formaton     [ OK ]");
            
            Vector<TaggedBrowseHistoryEntity> bEntities = delReader.getTaggedObjects();
            if(DbInterface.purgeTaggedEntitiesToDb(bEntities)) {
                System.out.println("Del.icio.us: Bookmarks purge     [ OK ]");
            } else {
                System.out.println("Del.icio.us: Bookmarks purge     [ FAILED ]");
            }
        } else {
            System.out.println("Del.icio.us: document formaton     [ FAILED ]");
        }
    }

    public static void importGoogleWebHistory() {
        ConfigManager mgr = ConfigManager.getInstance();
        
        // Read and purge the GoogleHistory File
        GoogleWebHistoryXMLReader gwhReader = new GoogleWebHistoryXMLReader(mgr.getValueForKey(Constants.KEY_FOR_GOOGLE_HISTORY_XML_PATH));
        if(gwhReader.formDocuments()) {
            System.out.println("GoogleHistory: document formaton     [ OK ]");
            
            Vector<BrowseHistoryEntity> bEntities = gwhReader.getBrowseResultObjects();
            if(DbInterface.purgeBrowseEntitiesToDb(bEntities)) {
                System.out.println("GoogleHistory: Browse results purge     [ OK ]");
            } else {
                System.out.println("GoogleHistory: Browse results purge     [ FAILED ]");
            }
            
            Vector<SearchHistoryEntity> sEntities = gwhReader.getWebQueryObjects();
            if(DbInterface.purgeSearchEntitiesToDb(sEntities)) {
                System.out.println("GoogleHistory: Search results purge     [ OK ]");
            } else {
                System.out.println("GoogleHistory: Browse results purge     [ FAILED ]");
            }
        } else {
            System.out.println("GoogleHistory: document formaton     [ FAILED ]");
        }
    }   
}
